﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLKS10_11.BLL;

namespace QLKS10_11.GUI
{
    public partial class form_Thietbi : Form
    {
        public form_Thietbi()
        {
            InitializeComponent();
        }
        /**********************************CHUYEN FORM************************************************/
        private static form_Thietbi _instance;
        public static form_Thietbi Instance()
        {
            if (_instance == null)
            {
                _instance = new form_Thietbi();
            }
            return _instance;
        }
        
        private void btn_thietbi_Click(object sender, EventArgs e)
        {
            form_main frm1 = form_main.Instance();
            frm1.Show();
            this.Hide();
        }

        private void form_Thietbi_FormClosed(object sender, FormClosedEventArgs e)
        {

            form_main frm1 = form_main.Instance();
            frm1.Dispose();
        }

         /******************************************LOAD DU LIEU TU DATABASE*********************************************************/
    
        Thietbi_BLL thietbi = new Thietbi_BLL();
        private void form_Thietbi_Load(object sender, EventArgs e)
        {
            thietbi.HienThi(gc_thietbi,bd_thietbi);
            //khi form load len khong cho nguoi dung tu sua danh sach
            gridView1.OptionsBehavior.Editable = false;
            btn_luu.Enabled = false;
        }

        private void btn_them_Click(object sender, EventArgs e)
        {
            btn_luu.Enabled = true;
            gridView1.OptionsBehavior.Editable = true;
            DataRow row = thietbi.NewRow();
            row["matb"] = "";
            row["tentb"] = "";
            thietbi.Add(row);
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            btn_luu.Enabled = true;
            if (gridView1.RowCount > 0)
            {
                if (MessageBox.Show("Ban co chat muon xoa thiet bi khong ?", "Xoa thiet bi", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    bd_thietbi.BindingSource.RemoveCurrent();
                    thietbi.Save();
                }
            }
            else MessageBox.Show("Dach sach thiet bi rong.");
        }

        private void btn_sua_Click(object sender, EventArgs e)
        {
            btn_luu.Enabled = true;
            gridView1.OptionsBehavior.Editable = true;
        }

        private void btn_luu_Click(object sender, EventArgs e)
        {
            bd_thietbi.BindingSource.MoveNext();
            bindingNavigatorPositionItem.Focus();
            thietbi.Save();
            MessageBox.Show("Luu thanh cong");
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /********************************************************************************************************************/
    }
}
